#include <sys/socket.h>
#include <arpa/inet.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>
#include "tlv.h"

#define ARGS_CHECK(argc,expect){if(argc!=expect){printf("ARGC NUM IS WRONG!\n");exit(-1);}}

void *callback(tlv_t *tlv,int sockfd,uint8_t type,uint32_t length,const uint8_t *value){
    printf("type=%d,length=%d,value=%s\n",type,length,value);
    return NULL;
}
int main(int argc, char *argv[])
{
    // ./client 192.168.159.128 12345
    ARGS_CHECK(argc,3);
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    addr.sin_port=htons(atoi(argv[2]));
    if(connect(sockfd,(struct sockaddr*)&addr,sizeof(addr))==-1){
        perror("connect");
        return -1;
    }

    tlv_t tlv;
    char message[]="test message.";

    if(tlv_request(&tlv,sockfd,REQUEST_CMD_EXIT,sizeof(message),(const uint8_t*)message,callback)==-1){
        fprintf(stderr,"tlv_request:%s\n",tlv_error_str(&tlv));
    }

    return 0;
}
